//
//  NSString+JKExtension.m
//  Category
//
//  Created by jamalping on 16/4/6.
//  Copyright © 2016年 jamalping. All rights reserved.
//

#import "NSString+JKExtension.h"

@implementation NSString (JKExtension)

/**
 *  格式化时间
 */
- (NSDate *)toNSDate {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [formatter dateFromString:self];
    return date;
}

- (NSDate *)toLongDate {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyyMMddHHmmss"];
    NSDate *date = [formatter dateFromString:self];
    return date;
}

// 时间处理
- (NSString *)toFormatDateString {
    NSDate *date = [self toLongDate];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy年MM月dd日 HH:mm"];
    
    NSLocale *local = [NSLocale currentLocale];
    [formatter setLocale:local];
    
    NSString *stringDate = [formatter stringFromDate:date];
    
    return stringDate;
}

- (NSString *)toFormatShortDateString {
    NSDate *date = [self toLongDate];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    
    NSLocale *local = [NSLocale currentLocale];
    [formatter setLocale:local];
    
    NSString *stringDate = [formatter stringFromDate:date];
    
    return stringDate;
}

// 修剪字符串（去除空格）
- (NSString *)trimWhiteSpace {
    return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}

// 以length为长度拆分成数组
- (NSArray *)splitToStringArrayByLength:(NSUInteger)length {
    NSMutableArray *cArray = [NSMutableArray array];
    int count = (int)[self length];
    NSRange range;
    for (int i = 0; i < count; i += length) {
        range.length = length;
        range.location = i;
        [cArray addObject:[self substringWithRange:range]];
    }
    return cArray;
}

// 替换字符串中的某个字符
- (NSString *)replaceStringBy:(NSString *)sor withString:(NSString*)dest {
    NSString *result = [self stringByReplacingOccurrencesOfString:sor withString:dest];
    if ([result rangeOfString:sor].length > 0){
        return [self replaceStringBy:sor withString:dest];
    }
    return result;
}

// 替换字符串（根据正则表达式）
- (NSString *)replaceAll:(NSString *)regexStr withString:(NSString*)dest {
    NSString *result = self;
    NSError *error = nil;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexStr options:0 error:&error];
    if (regex) {
        NSTextCheckingResult *firstMatch = [regex firstMatchInString:result options:0     range:NSMakeRange(0, result.length)];
        while (firstMatch) {
            NSRange range = [firstMatch rangeAtIndex:0];
            result = [result stringByReplacingCharactersInRange:range withString:dest];
            firstMatch = [regex firstMatchInString:result options:0 range:NSMakeRange(0, result.length)];
        }
    }
    return result;
}

// 组合字符串，
- (NSString *)commonString:(NSString *)str length:(int)len {
    if(self.length<1){
        return self;
    }
    NSMutableString *result = [NSMutableString string];
    for (int index=0; index<self.length; ) {
        if(index+len >= self.length) {
            // 余下字符已不足len或刚好，则加上余下的字符
            [result appendString:[self substringFromIndex:index]];
            break;
        }
        NSRange range = NSMakeRange(index, len);
        NSString *newStr = [self substringWithRange:range];
        [result appendFormat:@"%@%@",newStr,str];
        index += len;
    }
    
    return result;
}

@end
